<?php
/**
 * ソートヘルパ イテレータ
 * 
 * @author tanaka
 * @package View_Helper_Url
 * @subpackage Sort_Iterator
 */
class View_Helper_Url_Sort_Iterator extends View_Helper_Url_Sort implements IteratorAggregate
{
	/**
	 * オーダーの値
	 * @var array
	 */
	protected $_orderParams = array();

	/**
	 * 見出し
	 * @var array
	 */
	protected $_orderLabels = array();
	
	/**
	 * コンストラクタ
	 *
	 * @param array $orderParams URLに与えるパラメータ名の配列
	 * @param array $orderLabels リンクのテキスト
	 * @param string optional $indexKey
	 * @param string optional $specKey
	 * @return
	 */
	public function __construct($orderParams, $orderLabels, $orderKey = null, $specKey = null)
	{
		$this->_orderParams = $orderParams;
		$this->_orderLabels = $orderLabels;
		
		parent::__construct($orderKey, $specKey);
	}

	/**
	 * URL文字列を配列で返す
	 *
	 * @return array
	 */
	public function getUrlArray()
	{
		$result = array();

		foreach ($this->_orderLabels as $sequence => $label) {
			if (!isset($this->_orderParams[$sequence]) || !strlen($this->_orderParams[$sequence])){
				$result[] = $label;
				continue;
			}
			
			$result[] = $this->getUrl($label, $this->_orderParams[$sequence]);
		}

		return $result;
	}

	/**
	 * (non-PHPdoc)
	 * @see IteratorAggregate::getIterator()
	 */
	public function getIterator()
	{
		return new ArrayIterator($this->getUrlArray());
	}
}
